package com.freesun.shop.controller;

import com.freesun.shop.entity.Category;
import com.freesun.shop.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/prod/category")
public class ProdCategroyController {

    private static Logger logger = LoggerFactory.getLogger(ProdCategroyController.class);

    @Reference(check = false)
    private CategoryService categoryService;

    @ApiOperation("查询所有商品分类")
    @GetMapping("/table")
    public ResponseEntity<List<Category>> table() {
        List<Category> list = categoryService.list();
        return ResponseEntity.ok(list);
    }

    @ApiOperation("删除商品的分类菜单")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        logger.info("要删除的商品分类ID为:{}", id);
        categoryService.removeById(id);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("查询所有的父节点")
    @GetMapping("/listCategory")
    public ResponseEntity<List<Category>> listParent() {
        List<Category> listParent = categoryService.listParent();
        return ResponseEntity.ok(listParent);
    }

    @ApiOperation("新增商品的分类菜单")
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody Category category) {
        categoryService.save(category);
        return ResponseEntity.ok().build();
    }

    @ApiOperation("查询单个")
    @GetMapping("/info/{id}")
    public ResponseEntity<Category> getById(@PathVariable Long id){
        Category category = categoryService.getById(id);
        return ResponseEntity.ok(category);
    }

    @ApiOperation("修改商品分类")
    @PutMapping
    public ResponseEntity<Void> update(@RequestBody Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryService.updateById(category);
        return ResponseEntity.ok().build();
    }
}

